package ru.reaperoq.inventory.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.rememberNavController
import org.koin.androidx.compose.navigation.koinNavViewModel
import ru.reaperoq.inventory.navigation.utils.LocalNavController
import splash.SplashViewModel

@Composable
fun MainNavigation(modifier: Modifier = Modifier, viewModel: SplashViewModel = koinNavViewModel()) {
    val navController = rememberNavController()

    CompositionLocalProvider(
        LocalNavController provides navController
    ) {
        NavHost(
            modifier = modifier,
            navController = navController,
            startDestination = if (viewModel.isTokenWritten()) {
                Screens.MainFlow.route
            } else {
                Screens.AuthFlow.route
            }
        ) {
            authFlow {
                navController.navigate(Screens.MainFlow.route)
            }
            mainFlow {
                navController.navigate(Screens.AuthFlow.route) {
                    popUpTo(Screens.MainFlow.route) {
                        inclusive = true
                    }
                }
            }
        }
    }
}